package com.jinyang.jetpackdemo.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import com.jinyang.jetpackdemo.R
import com.jinyang.jetpackdemo.util.LjyLogUtil
import kotlinx.coroutines.*

class LifecycleDemoActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_lifecycle_demo)
        lifecycle.addObserver(MyObserver())
        LjyLogUtil.d("onCreate:主线程id=${mainLooper.thread.id}")
        GlobalScope.launch {
            val result1 = GlobalScope.async {
                delay(1000)
                1
            }
            val result2 = GlobalScope.async {
                delay(4000)
                4
            }
            val result = result1.await() + result2.await()
            LjyLogUtil.d("onCreate:协程线程id=${Thread.currentThread().id},result = $result")
        }
    }

//
//    override fun onStart() {
//        super.onStart()
//        LjyLogUtil.d("onStart")
//    }
//
//    override fun onResume() {
//        super.onResume()
//        LjyLogUtil.d("onResume")
//    }
//
//    override fun onPause() {
//        super.onPause()
//        LjyLogUtil.d("onPause")
//    }
//
//    override fun onStop() {
//        super.onStop()
//        LjyLogUtil.d("onStop")
//    }
//
//    override fun onDestroy() {
//        super.onDestroy()
//        LjyLogUtil.d("onDestroy")
//    }
//
//    override fun onRestart() {
//        super.onRestart()
//        LjyLogUtil.d("onRestart")
//    }
}

class MyObserver : LifecycleObserver {
    //观察者的方法可以接受一个参数LifecycleOwner，就可以用来获取当前状态、或者继续添加观察者。
    //若注解的是ON_ANY还可以接收Event，用于区分是哪个事件。
    @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    fun onAny(owner: LifecycleOwner, event: Lifecycle.Event) {
        LjyLogUtil.d("LifecycleObserver.onAny:owner=" + owner.javaClass.name + ",event.name=" + event.name)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun onCreate() {
        LjyLogUtil.d("LifecycleObserver.onCreate")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun onCreate(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onCreate:owner=${owner.javaClass.name}")
        GlobalScope.launch {
            delay(5000)
            //回调后检查当前生命周期状态
            if (owner.lifecycle.currentState.isAtLeast(Lifecycle.State.CREATED)) {
                LjyLogUtil.d("LifecycleObserver.onCreate:协程线程id=${Thread.currentThread().id}")
            }
        }
    }

//    @OnLifecycleEvent(Lifecycle.Event.ON_START)
//    fun onStart() {
//        LjyLogUtil.d("LifecycleObserver.onStart")
//    }
//
//    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
//    fun onResume() {
//        LjyLogUtil.d("LifecycleObserver.onResume")
//    }
//
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun onPause(owner: LifecycleOwner) {
        LjyLogUtil.d("LifecycleObserver.onPause:${owner.lifecycle.currentState}")
    }
//
//    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
//    fun onStop() {
//        LjyLogUtil.d("LifecycleObserver.onStop")
//    }
//
//    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
//    fun onDestroy() {
//        LjyLogUtil.d("LifecycleObserver.onDestroy")
//    }
}
